package org.cs.mgr.before.ctl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.apache.tomcat.jni.Mmap;
import org.cs.mgr.before.model.ExcellentSelector;
import org.cs.mgr.before.model.Goods;
import org.cs.mgr.before.model.Goods_Specification;
import org.cs.mgr.before.model.User;
import org.cs.mgr.before.service.ICommentService;
import org.cs.mgr.before.service.IExcellentSelectorService;
import org.cs.mgr.before.service.IGoodsService;
import org.cs.mgr.before.service.IGoodsSpecificationService;
import org.cs.mgr.before.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;



/**
 * @Author:guo
 * @Date: 
 * @Description:
 */
@Controller
@RequestMapping("/reception")
public class DetailsCtl {
	
	@Autowired
	IGoodsService igoodsService;
	@Autowired
	IGoodsSpecificationService    igoodsSpecificationService;
	@Autowired
	ICommentService    icommentServcie;
	@Autowired
	IUserService iuserService;
	@Autowired
	IExcellentSelectorService iExcellentSelectorService;
	
	
	//跳转商品详情页面
    @RequestMapping("details")
    public String details(String id,ModelMap mm) {             //id是goods的ID
        Goods good = igoodsService.getGood(id);           //通过goods的id查goods表
        mm.put("good", good);
        //通过goods的id查goods_specification表
        List<Map<String, Object>> goods_SpecificationList = igoodsSpecificationService.getGoodsSpecification(id);                              
		mm.put("goodsspecification",goods_SpecificationList);
		//通过商品的id,comment表，user表，goods_specification表，三表联查
		List<Map<String, Object>> comments = icommentServcie.getComment(id);  				
		mm.put("comments",comments);
		//通过goods_id,查询excellent_selector表(优选师表),返回list
		List<Map<String, Object>> excellentSelectorOne = iExcellentSelectorService.getExcellentSelectorOne(id);    
		mm.put("esOne", excellentSelectorOne);
		System.out.println(excellentSelectorOne+"==========");
        return "jsp/details";
    }
    
    
    //商品详情页面的规格页面
    @RequestMapping("specification")
    @ResponseBody	
    public String specification(String id) {    	 //被选中的商品规格的id
    	JSONObject jsonObject = new JSONObject();
    	List<Map<String, Object>> goodsSpecificationOne = igoodsSpecificationService.getGoodsSpecificationOne(id);
    	jsonObject.put("json", goodsSpecificationOne.get(0));
        return jsonObject.toJSONString();
    }
    
  
   //访问优选师主页(优选师=寻味师)
   @RequestMapping("excellent_selector")
    public String excellent_selector(String id,ModelMap mm) {   //参数id是excellent_selector表(优选表)的ID
    	List<Map<String, Object>> excellentSelectorOne = iExcellentSelectorService.getExcellentSelectorOne(id);   //通过id查excellent_selector表
    	mm.put("esOne2", excellentSelectorOne);
    	//通过excellent_selector表的ID,查excellent_selector_recommend表和goods表，返回list
    	List<Map<String, Object>> excellentSelectorOne2 = iExcellentSelectorService.getRecommendGoods(id); 	    	
    	ArrayList<Map<String, Object>> list1 = new ArrayList<>();
    	//判断商品库存是否是0	
    	for (Map<String, Object> map : excellentSelectorOne2) {
			String people_number = (String) map.get("PEOPLE_NUMBER");    		
			int people_number1 = Integer.parseInt(people_number);
			if(people_number1==0){
				map.put("st", "已经售罄");
			}else{
				map.put("st", "正在进行时");
			}
		}
    	System.out.println("====="+excellentSelectorOne2);
    	mm.put("esOne3", excellentSelectorOne2);
    	return "jsp/details_people";
    }
    
}
